package club.ljwang.service;

import club.ljwang.constant.MessageConstant;
import club.ljwang.dao.OrderSettingMapper;
import club.ljwang.entity.Result;
import club.ljwang.pojo.OrderSetting;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Auther: wangl
 * @Date: 2020/2/29 20:09
 * @Description:
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Result setNumberByDate(OrderSetting orderSetting) {

        Integer count = orderSettingMapper.findNumberByDate(orderSetting.getOrderDate());

        if (count > 0) {

            orderSettingMapper.setNumberByDate(orderSetting);

        } else {

            orderSettingMapper.addNumberByDate(orderSetting);

        }

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    @Override
    public void addBatch(List<OrderSetting> orderSettings) {
        //能不能批量插入？不能每条数据入库之前都有逻辑判断
        for (OrderSetting orderSetting : orderSettings) {
            setNumberByDate(orderSetting);
        }
        //我们可以使用多线程导入数据（要把orderSettings拆成多份然后交给多个线程去干，
        // List的拆分方法就是使用Google Guava里面的Lists的工具类）
    }

    @Override
    public List<Map> findByDate(String date) {//2020-03
        List<Map> result = new ArrayList<>();
        String start = date + "-01";//2020-03-01
        String end = date + "-31";//2020-03-31
        List<OrderSetting> orderSettings = orderSettingMapper.findByDate(start,end);
        if(CollectionUtil.isNotEmpty(orderSettings)){
            for (OrderSetting orderSetting : orderSettings) {
                //{"date":22,"mouth":1,"number":300,"reservations":300,}
                Date orderDate = orderSetting.getOrderDate();
                Map map = new HashMap();
                map.put("date",orderDate.getDate());
                map.put("mouth",orderDate.getMonth());//字段错没事 只要前后端保持一致
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                result.add(map);
            }
        }
        return result;
    }
}
